//
//  DDropTimeModel.swift
//  Drop
//
//  Created by hushaohua on 2018/9/23.
//  Copyright © 2018年 init. All rights reserved.
//

import Foundation
import UIKit

class DDropTimeModel {
    var beginTime:Date?{
        didSet{ if (beginTime != nil) {self.getBeginDayTimeString();} }
    }
    var endTime:Date?{
        didSet{ if endTime != nil {self.getEndDayTimeStrings();} }
    }
    
    func getBeginDayTimeString() -> Void{
        let strings = self.defaultDateTimeString(self.beginTime!);
        self.beginDayString = strings.day;
        self.beginTimeString = strings.time;
        if self.timeFont != nil {
            self.beginTimeSize = self.sizeForString(self.beginTimeString!, font: self.timeFont!);
        }
        if self.dayFont != nil && self.beginDayString != nil {
            self.beginDaySize = self.sizeForString(self.beginDayString!, font: self.dayFont!);
        }
        self.getDurationIf();
    }
    
    func getEndDayTimeStrings() -> Void{
        let strings = self.defaultDateTimeString(self.endTime!);
        self.endDayString = strings.day;
        self.endTimeString = strings.time;
        if self.timeFont != nil {
            self.endTimeSize = self.sizeForString(self.endTimeString!, font: self.timeFont!);
        }
        if self.dayFont != nil && self.endDayString != nil {
            self.endDaySize = self.sizeForString(self.endDayString!, font: self.dayFont!);
        }
        self.getDurationIf();
    }
    
    var durationString:NSString?;
    var durationSize:CGSize?;
    var durationSeconds:TimeInterval = 0;
    
    func getDurationIf() {
        if (self.beginTime != nil && self.endTime != nil){
            self.durationSeconds = self.endTime!.timeIntervalSince(self.beginTime!);
            let interval:Int64 = Int64(self.durationSeconds)
            let hours:Int64 = interval / 3600;
            let minutes:Int64 = (interval - hours * 3600) / 60;
            let durationString:String = "\(hours):\(minutes)";
            self.durationSize = self.sizeForString(durationString, font: self.dayFont!);
            self.durationString = durationString as NSString;
        }
    }
    
    func defaultDateTimeString(_ date:Date) -> (day:String?, time:String?){
//        let string = date.defaultDateTimeString();
        let dateFormatter = DateFormatter();
        dateFormatter.dateFormat = "MM-dd HH:mm";
        let string = dateFormatter.string(from: date);
        let strings = string.components(separatedBy: " ");
        if (strings.count > 1){
            return (strings.first!, strings.last!);
        }else{
            return (nil, string);
        }
    }
    var beginDayString:String?;
    var beginTimeString:String?;
    var endDayString:String?;
    var endTimeString:String?;
    
    var beginTimeSize:CGSize;
    var endTimeSize:CGSize;
    var beginDaySize:CGSize;
    var endDaySize:CGSize;
    
    init() {
        self.beginTimeSize = CGSize();
        self.endTimeSize = CGSize();
        self.beginDaySize = CGSize();
        self.endDaySize = CGSize();
    }
    
    func sizeForString(_ string:String, font:UIFont) -> CGSize{
        let string = NSString(string: string);
        var size = string.size(withAttributes: [NSAttributedStringKey.font : font]);
        size.width = CGFloat(ceilf(Float(size.width)) + 1);
        size.height = CGFloat(ceilf(Float(size.height)));
        return size;
    }
    
    var timeFont:UIFont?;
    var dayFont:UIFont?;
}
